import APIConfig from "../config/api"
import {
  wxToPromise
} from "./wx"
import exceptionMessage from "../config/exception-message"
class Http {
  static async request({
    url,
    data,
    method = "GET"
  }) {
    wx.showLoading({
      title: '加载中',
    })
    let res = await wxToPromise("request", {

      url: APIConfig.baseUrl + url,
      data,
      method
    })
    if (res.statusCode < 400) {
      wx.hideLoading()
      return res.data.data
    }
    if (res.statusCode === 401) {
      wx.hideLoading()
      return
    }
    wx.hideLoading()
    Http._showError(res.data.error_code, res.data.message)
  }
  static _showError(errorCode, message) {
    let title
    const errorMessage = exceptionMessage[errorCode]
    title = errorMessage || message || "未知异常"
    title = typeof title === "object" ? object.values(title).join() : title
    wx.showToast({
      title,
      icon: 'none',
      duration: 3000
    })
  }
}
export default Http